"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFFkdCtrl
 * @description
 * # FinanceFFkdCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("FinanceFFkdCtrl", function(
    $scope,
    $rootScope,
    openApi,
    Util,
    $routeParams,
    FormSubmit
  ) {
    var id = ($scope.process_id = $routeParams.id);
    $scope.payWayList = [
      {
        key: "现金",
        value: "现金"
      },
      {
        key: "转账",
        value: "转账"
      },
      {
        key: "汇票",
        value: "汇票"
      },
      {
        key: "其他",
        value: "其他"
      }
    ];
    $scope.getpaytype = function() {
      var param = {};
      param.type = "付款类型";
      Util.getKeyValue(param, function(data) {
        $scope.payTypeList = data.data;
      });
    };
    $scope.showSearchPicker = function() {
      $scope.searchtitle = "收款人";
      $scope.apiurl = openApi.getAccountHistoryRecord;
      $scope.showSearchInput = true;
      $rootScope.$emit("showmasker");
    };
    $scope.handleSearch = function(data) {
      $scope.param.acceptCompany = data[0];
      $scope.param.acceptAccount = data[1];
      $scope.param.acceptBank = data[2];
      hideSearchPicker();
    };
    var hideSearchPicker = function() {
      $rootScope.$emit("closemasker");
      $scope.showSearchInput = false;
    };
    $scope.$on("closemasker", function() {
      hideSearchPicker();
    });
    // 选择付款单明细
    $scope.showCertificate = function() {
      $rootScope.$emit("showmasker");
      $scope.showCerPicker = true;
    };
    $scope.deletedItem = function(index) {
      $scope.param.paymentList.splice(index, 1);
      $scope.index = -1;
    };
    $scope.addCertificate = function() {
      $scope.picker = $scope.picker || {};
      var check = false;
      check = checkPicker($scope.picker);
      if (!check) {
        return;
      }
      $scope.param.paymentList.push($scope.picker);
      // 隐藏表单
      hideCertificate();
    };
    var hideCertificate = function() {
      $scope.picker = {};
      $rootScope.$emit("closemasker");
      $scope.showCerPicker = false;
    };
    $scope.$on("closemasker", function() {
      hideCertificate();
    });
    $scope.cacelCertificate = function() {
      hideCertificate();
    };
    $scope.swipe = function(index) {
      $scope.index = index;
    };
    $scope.resetlist = function() {
      $scope.index = -1;
    };
    // 验证是否输入完整
    var checkPicker = function(data) {
      var check = true;
      if (!data.type) {
        Util.Toast("请选择付款类型");
        check = false;
        return check;
      }
      if (!data.amount) {
        Util.Toast("请填写付款金额");
        check = false;
        return check;
      }
      if (!data.payReason) {
        Util.Toast("请填写付款事由");
        check = false;
        return check;
      }
      if (!data.acceptCompany) {
        Util.Toast("请填写收款人");
        check = false;
        return check;
      }
      if (!data.acceptAccount) {
        Util.Toast("请填写收款账号");
        check = false;
        return check;
      }
      if (!data.acceptBank) {
        Util.Toast("请填写收款人开户行");
        check = false;
        return check;
      }
      return check;
    };

    $scope.submit = function(operationId) {
      $scope.param.payments = angular.toJson($scope.param.paymentList);
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.payMoney;
      options.param = angular.copy($scope.param, {});
      delete options.param.paymentList
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function() {
      if (!$scope.param.type) {
        Util.Toast("请填写款项类型");
        return;
      }
      if (!$scope.param.method) {
        Util.Toast("请填写付款方式");
        return;
      }
      if (!$scope.param.amount) {
        if ($scope.param.amount !== 0) {
          Util.Toast("请填写付款金额");
          return;
        }
      }
      if (!$scope.param.acceptCompany) {
        Util.Toast("请填写收款人");
        return;
      }
      if (!$scope.param.acceptAccount) {
        Util.Toast("请填写收款人账号");
        return;
      }
      if (!$scope.param.acceptBank) {
        Util.Toast("请填写收款人开户行");
        return;
      }
      if (
        $scope.param.relatedModuleName &&
        $scope.param.relatedModuleName !== "无"
      ) {
        if (!$scope.param.moduleNo) {
          Util.Toast("请选择模块编号");
          return;
        }
      }
      if (!$scope.param.payReason) {
        Util.Toast("请填写付款事由");
        return;
      }
      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function() {
      // 获取回显的详细信息
      Util.getDetailData(id, function(data) {
        $scope.param = data;
        $scope.param.paymentList = $scope.param.paymentList || [];
      });
      $scope.getpaytype();
    };
    // 初始化
    init();
  });
